﻿
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Interactivity;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Data;
using System.Linq;
using Caliburn.Micro;

namespace Phone.Framework.Behaviors
{

    public class NoItemsSourceTipBehavior : Behavior<Control>
    {
        DependencyPropertyListener _listener = new DependencyPropertyListener();

        private TextBlock tip = new TextBlock() { Text = "No Results", 
            Foreground = ThemeResources.PhoneDisabledBrush, FontSize = ThemeResources.PhoneFontSizeExtraLarge };
        private ContentControl contentControl = new ContentControl() { Visibility = Visibility.Collapsed,VerticalAlignment= VerticalAlignment.Top };
        protected override void OnAttached()
        {
            _listener.Attach(this, 
                new Binding("ItemsSource") { Source = AssociatedObject, Mode = BindingMode.TwoWay });
            _listener.Changed += new System.EventHandler<BindingChangedEventArgs>(_listener_Changed);
            //BindingOperations.SetBinding(this, ItemsSourceProperty, 
            //    new Binding("ItemsSource") { Source = AssociatedObject, Mode = BindingMode.TwoWay });
            if (DataTemplate != null)
                contentControl.ContentTemplate = DataTemplate;
            else
                contentControl.Content = tip;

            base.OnAttached();
        }

        protected override void OnDetaching()
        {
            _listener.Detach();
            base.OnDetaching();
        }

        private bool isTipAdded = false;

        void _listener_Changed(object sender, BindingChangedEventArgs e)
        {

            var obj = AssociatedObject.GetType().GetProperty("ItemsSource").GetValue(AssociatedObject, null) as IEnumerable;
            if (obj == null || obj.Cast<System.Object>().Count() == 0)
            {
                if (!isTipAdded)
                {
                    var panel = AssociatedObject.Parent as Panel;
                    if (panel != null)
                    {
                        panel.Children.Add(contentControl);
                    }
                   
                }
                AssociatedObject.Visibility = Visibility.Collapsed;
                contentControl.Visibility = Visibility.Visible;
                isTipAdded = true;
            }
            else
            {
                if (isTipAdded)
                {
                    AssociatedObject.Visibility = Visibility.Visible;
                    contentControl.Visibility = Visibility.Collapsed;
                }
            }
        }

        public DataTemplate DataTemplate
        {
            get { return (DataTemplate)GetValue(DataTemplateProperty); }
            set { SetValue(DataTemplateProperty, value); }
        }

     
        public static readonly DependencyProperty DataTemplateProperty =
            DependencyProperty.Register("DataTemplate", typeof(DataTemplate), typeof(NoItemsSourceTipBehavior), 
            new PropertyMetadata(null));

        

    }


    public class BindListBoxItemActionBehavior : Behavior<FrameworkElement>
    {
      
        protected override void OnAttached()
        {
            AssociatedObject.Loaded += new RoutedEventHandler(AssociatedObject_Loaded);

            base.OnAttached();
        }

        void AssociatedObject_Loaded(object sender, RoutedEventArgs e)
        {
            AssociatedObject.Loaded -= new RoutedEventHandler(AssociatedObject_Loaded);
            var element = AssociatedObject.FindAncestor<ListBoxItem>();
            if (element != null)
            {
                element.SetValue(Action.TargetWithoutContextProperty,
                    Action.GetTargetWithoutContext(AssociatedObject));
                element.SetValue(Message.AttachProperty,Message.GetAttach(AssociatedObject));
                AssociatedObject.SetValue(Action.TargetWithoutContextProperty, null);
                AssociatedObject.SetValue(Message.AttachProperty, string.Empty);
            }
        }

        protected override void OnDetaching()
        {
            
            base.OnDetaching();
        }

       


    }
}
